import { createStore } from 'vuex'

const user = {
	namespaced: true,
	state() {
		return {
			isLogin: false,
			userInfo: {}
		}
	},
	mutations: {
		SET_USER(state, data) {
			if (!data) return
			state.userInfo = data
			state.isLogin = true
		}
	},
	actions: {
		
		// 获取用户信息
		async getUserInfo({ commit, dispatch }) {
			if(!uni.getStorageSync('uni_id_token')) return
			
			const db = uniCloud.database();
			let {result} = await db.collection('uni-id-users').where({_id:db.env.uid}).field('username,nickname').get({getOne:true})
			if(result.code==0) commit('SET_USER',result.data)
			else{
				uni.showToast({
					icon:'none',
					title:'获取用户信息失败'
				})
				setTimeout(()=>{
					commit('getUserInfo')
				},1000)
			}
		},

	},
}
export default user
